package com.wffz.config.security;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wffz.mapper.ClientUserMapper;
import com.wffz.po.ClientUser;
import com.wffz.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @author sunlei
 * @className LoginSuccessHandler
 * @Description
 * @date 2022-12-27 14:01
 **/
@Component("loginSuccessHandler")
@Slf4j
public class LoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Resource
    private ObjectMapper objectMapper;

    private RequestCache requestCache;

    @Resource
    private UserServiceImpl userService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
        // 获取前端传到后端的全部参数
        Enumeration enu = request.getParameterNames();
        String userName = null;
        while (enu.hasMoreElements()) {
            String paraName = (String) enu.nextElement();
            if (paraName.equals("username")) {
                userName = request.getParameter(paraName);
            }
//            System.out.println("参数- " + paraName + " : " + request.getParameter(paraName));
        }
        ClientUser clientUser = userService.findUserByUserName(userName);
        HttpSession session = request.getSession();
        session.setAttribute("loginUser", clientUser);
        log.info("登录认证成功");
        //这里写你登录成功后的逻辑，可以验证其他信息，如验证码等。
        response.setContentType("application/json;charset=UTF-8");
        JSONObject resultObj = new JSONObject();
        resultObj.put("code", HttpStatus.OK.value());
        resultObj.put("msg", "登录成功");
        resultObj.put("success",true);
        resultObj.put("authentication", objectMapper.writeValueAsString(authentication));
//        System.out.println(resultObj.toString());
        response.getWriter().write(resultObj.toString());
//        this.getRedirectStrategy().sendRedirect(request, response, "/mime");//重定向
    }
}